<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <!-- hexo-inject:begin --><!-- hexo-inject:end --><meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/avatar.jpg?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/avatar.jpg?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/avatar.jpg?v=5.1.3">


  <link rel="mask-icon" href="/images/avatar.jpg?v=5.1.3" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="学习 | 分享 | 交流 | 进步">
<meta property="og:type" content="website">
<meta property="og:title" content="Fighter&#39;s Blog">
<meta property="og:url" content="http://fighterhit.github.io/page/2/index.html">
<meta property="og:site_name" content="Fighter&#39;s Blog">
<meta property="og:description" content="学习 | 分享 | 交流 | 进步">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Fighter&#39;s Blog">
<meta name="twitter:description" content="学习 | 分享 | 交流 | 进步">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://fighterhit.github.io/page/2/"/>





  <title>Fighter's Blog</title><!-- hexo-inject:begin --><!-- hexo-inject:end -->
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <!-- hexo-inject:begin --><!-- hexo-inject:end --><div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Fighter's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">深度沉迷学习</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://fighterhit.github.io/2018/02/17/Java_Notes/Jvm/类文件/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Fighter.">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Fighter's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/02/17/Java_Notes/Jvm/类文件/" itemprop="url">类文件</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-02-17T23:52:32+08:00">
                2018-02-17
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java笔记/" itemprop="url" rel="index">
                    <span itemprop="name">Java笔记</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="class-文件结构"><a href="#class-文件结构" class="headerlink" title="class 文件结构"></a>class 文件结构</h2><ul>
<li>class 文件是一组以 8 位字节为基础单位的二进制流，各个数据项目严格按照顺序紧凑的排列在 class 文件之中，中间没有添加任何分隔符，整个 class 文件中存储的内容几乎全部是程序运行的必要数据，没有空隙存在。</li>
<li>当遇到 8 位字节以上的空间的数据项时，则会按照高位在前的方式分割成若干个 8 位字节进行存储。</li>
<li>class 文件中有两种数据类型，分别是无符号数和表（引用类型）。</li>
</ul>
<blockquote>
<ul>
<li>魔数</li>
<li>class 文件版本</li>
<li>常量池</li>
<li>访问标志</li>
<li>类索引，父类索引，接口索引集合</li>
<li>字段表集合</li>
<li>方法表集合</li>
<li>属性表集合</li>
</ul>
</blockquote>
<h3 id="魔数"><a href="#魔数" class="headerlink" title="魔数"></a>魔数</h3><ul>
<li>版本</li>
<li>JDK 1.8=52</li>
<li>JDK 1.7=51</li>
<li>JDK 1.6=50</li>
<li>JDK 1.5=49</li>
<li>JDK 1.4=48</li>
<li>JDK 1.3=47</li>
<li>JDK 1.2=46</li>
<li>JDK 1.1=45</li>
</ul>
<p>class 文件以 cafebabe 开头，后面跟着的是 JDK 次版本号和主版本号</p>
<h3 id="常量池"><a href="#常量池" class="headerlink" title="常量池"></a>常量池</h3><ul>
<li><p>CP_info</p>
</li>
<li><p>CONSTANT_Methodref_info 用于记录方法信息（包括类中定义的方法以及代码中使用到的方法）</p>
</li>
</ul>
<blockquote>
<p>查看 class 文件信息：javap -verbose HelloWorld.class</p>
</blockquote>
<h3 id="访问标志"><a href="#访问标志" class="headerlink" title="访问标志"></a>访问标志</h3><h3 id="类索引"><a href="#类索引" class="headerlink" title="类索引"></a>类索引</h3><h3 id="字段表集合"><a href="#字段表集合" class="headerlink" title="字段表集合"></a>字段表集合</h3><p>字段表用于描述接口或者类中声明的变量</p>
<h3 id="方法表集合"><a href="#方法表集合" class="headerlink" title="方法表集合"></a>方法表集合</h3><h3 id="属性表集合"><a href="#属性表集合" class="headerlink" title="属性表集合"></a>属性表集合</h3><h2 id="字节码指令"><a href="#字节码指令" class="headerlink" title="字节码指令"></a>字节码指令</h2><ul>
<li>Java 虚拟机的指令由一个字节长度的，代表着某种特定操作含义的数字，称之为操作码，以及跟随其后的零至多个代表此操作所需参数的操作数而构成。</li>
<li>操作码长度为 1 个字节，因此最大只有 256 条。</li>
<li>基于栈的指令集架构</li>
</ul>
<h3 id="字节码与数据类型"><a href="#字节码与数据类型" class="headerlink" title="字节码与数据类型"></a>字节码与数据类型</h3><ul>
<li>在虚拟机的指令集中，大多数的指令都包含了其操作所对应的数据类型信息。</li>
<li>lload fload</li>
<li>大多数指令是包含类型信息的</li>
<li>也有不包含类型信息的<ul>
<li>goto 与类型无关</li>
<li>Arraylength 操作数组类型</li>
</ul>
</li>
<li>类型多，指令少</li>
</ul>
<h3 id="加载和存储指令"><a href="#加载和存储指令" class="headerlink" title="加载和存储指令"></a>加载和存储指令</h3><ul>
<li>加载和存储指令用于将数据在栈帧中的局部变量表和操作数栈之间来回传输。</li>
<li>将局部变量表加载到操作数栈：iload lload fload dload aload</li>
<li>将一个数值从操作数栈存储到局部变量表：istore lfda</li>
<li>将一个常量加载到操作数栈：bipush sipush ldc ldc_w_ldc2_w aconst_null iconst_m1 iconst</li>
<li>扩充局部变量表的访问索引的指令：wide</li>
</ul>
<h3 id="运算指令"><a href="#运算指令" class="headerlink" title="运算指令"></a>运算指令</h3><ul>
<li>运算或算术指令用于对两个操作数栈上的值进行某种特定的运算，并把结果存储到操作数栈顶。</li>
<li>加法指令：add</li>
<li>减法指令：sub</li>
<li>乘法指令：mul</li>
<li>除法指令：div</li>
<li>取余指令：rem</li>
<li>取反指令：neg</li>
</ul>
<h3 id="类型转换指令"><a href="#类型转换指令" class="headerlink" title="类型转换指令"></a>类型转换指令</h3><ul>
<li>类型转换指令可以将两种不同的数值类型进行相互转换，这些转换操作一般用于实现用户代码中的显示类型转换操作以及用来处理字节码指令集中数据类型相关指令无法与数据类型一一对应的问题。</li>
<li>宽化类型处理和窄化类型处理</li>
<li>i2b i2c i2s l2i</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">问题：</div><div class="line">Animal animal = getAnimal()</div><div class="line">Object obj = animal;</div><div class="line">User u = (User) obj;</div></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">int</span> hour = <span class="number">24</span>;</div><div class="line"><span class="comment">//计算过程都按 int 计算，int 越界</span></div><div class="line"><span class="keyword">long</span> mi = hour * <span class="number">60</span> * <span class="number">60</span> * <span class="number">1000</span>;</div><div class="line"><span class="keyword">long</span> mic = hour * <span class="number">60</span> * <span class="number">60</span> * <span class="number">1000</span>*<span class="number">1000</span>;</div><div class="line"><span class="comment">//最后执行一次 i2l</span></div><div class="line">System.out.println(mic / mi);</div></pre></td></tr></table></figure>
<h3 id="对象创建与访问指令"><a href="#对象创建与访问指令" class="headerlink" title="对象创建与访问指令"></a>对象创建与访问指令</h3><ul>
<li>创建类实例的指令：new</li>
<li>创建数组的指令：newarray anewarray multianewarray</li>
<li>访问类字段：getfield putfield getstatic putstatic</li>
<li>把数组元素加载到操作数栈的指令：baload c s i l f d a</li>
<li>将操作数栈的值存储到数组元素：astore</li>
<li>取数组长度的指令：arraylength</li>
<li>检查实例类型的指令：instanceof checkcast</li>
</ul>
<h3 id="操作数栈管理指令"><a href="#操作数栈管理指令" class="headerlink" title="操作数栈管理指令"></a>操作数栈管理指令</h3><ul>
<li>操作数栈指令用于直接操作操作数栈</li>
<li>将操作数栈的一个或两个元素出栈：pop pop2</li>
<li>复制栈顶一个或两个数值并将复制或双份复制值重新压入栈顶：dup dup2 dup_x1 dup_x2</li>
<li>将栈顶的两个数值替换：swap</li>
</ul>
<h3 id="控制转移指令"><a href="#控制转移指令" class="headerlink" title="控制转移指令"></a>控制转移指令</h3><ul>
<li>控制转移指令可以让 Java 虚拟机有条件或无条件的从指定的位置指令而不是控制转移指令的下一条指令继续执行程序。可以认为控制转移指令就是在修改 pc 寄存器的值。</li>
<li>条件分支：ifeq iflt ifle ifne ifgt ifnull ifcmple</li>
<li>复合条件分支：tableswitch lookupswitch</li>
<li>无条件分支：goto goto_w jsr jsr_w ret</li>
</ul>
<h3 id="方法调用和返回指令"><a href="#方法调用和返回指令" class="headerlink" title="方法调用和返回指令"></a>方法调用和返回指令</h3><h4 id="方法调用"><a href="#方法调用" class="headerlink" title="方法调用"></a>方法调用</h4><ul>
<li>invokevirtual 指令用于调用对象的实例方法，根据对象的实际类型进行分派（虚方法分派），这也是 Java 语言中最常见的方法分派方式。</li>
<li>invokeinterface 指令用于调用接口方法，它会在运行时搜索一个实现了这个接口方法的对象，找出适合的方法进行调用。</li>
<li>invokespecial 指令用于调用一些需要特殊处理的实例方法，包括实例初始化方法、私有方法和父类方法。</li>
<li>invokestatic 指令用于调用类方法（static 方法）</li>
</ul>
<h4 id="方法的返回指令"><a href="#方法的返回指令" class="headerlink" title="方法的返回指令"></a>方法的返回指令</h4><ul>
<li>方法的调用指令与数据类型无关，而方法返回指令则是根据返回值的类型区分的，包括 ireturn（当返回值是 boolean、byte、char、short 和 int 类型时使用）、lreturn、freturn、dreturn 和 areturn，另外还有一条 return 指令供声明为 void 的方法、实例初始化方法、类和接口的类初始化方法使用。</li>
</ul>
<h3 id="异常处理指令"><a href="#异常处理指令" class="headerlink" title="异常处理指令"></a>异常处理指令</h3><p>在程序中<strong>显式抛出异常的操作</strong>会由 athrow 指令实现，除了这种情况，还有别的异常会在其他 Java 虚拟机指令检测到异常状况时由虚拟机自动抛出。</p>
<p>新 jdk 不再使用字节码指令执行 catch 语句了，而使用 exception table 执行 catch 下语句。</p>
<p><a href="http://www.itwendao.com/article/detail/335271.html" target="_blank" rel="noopener">Java虚拟机是如何处理异常的？</a><br><a href="https://www.javaworld.com/article/2076868/learn-java/how-the-java-virtual-machine-handles-exceptions.html" target="_blank" rel="noopener">How the Java virtual machine handles exceptions
</a></p>
<h3 id="同步指令"><a href="#同步指令" class="headerlink" title="同步指令"></a>同步指令</h3><h4 id="同步指令-1"><a href="#同步指令-1" class="headerlink" title="同步指令"></a>同步指令</h4><ul>
<li>Java 虚拟机可以支持方法级的同步和方法内部一段指令序列的同步，这两种同步结构都是使用管程（monitor）来支持的。</li>
<li>方法级的同步是隐式的，即无需通过字节码指令来控制，它实现在方法调用和返回操作中。虚拟机可以从方法常量池中的方法表结构（method info structure）中的 ACC_SYNCHRONIZED 访问标志是否被设置，如果设置了，执行线程将先持有管程，然后再执行方法，最后在方法完成（无论正常完成还是非正常完成）时释放管程。在方法执行期间，执行线程持有了管程，其他任何线程都无法再获得同一个管程。如果一个同步方法执行期间抛出了异常，并在方法内部无法处理此异常，那这个同步方法所持有的管程将在异常抛到同步方法外时自动释放。</li>
<li>同步一段指令集序列通常是由 Java 语言中的 synchronized 块表示，Java 虚拟机的指令集中有 monitorenter 和 monitorexit 两条指令来支持 synchronized 关键字的语义，正确实现 synchronized 关键字需要编译器与 Java 虚拟机两者协作支持。</li>
</ul>
<h4 id="管程"><a href="#管程" class="headerlink" title="管程"></a>管程</h4><ul>
<li>管程 (英语：Monitors，也称为监视器) 是一种程序结构，结构内的多个子程序（对象或模块）形成的多个工作线程互斥访问共享资源。这些共享资源一般是硬件设备或一群变量。</li>
<li>管程实现了在一个时间点，最多只有一个线程在执行管程的某个子程序。与那些通过修改数据结构实现互斥访问的并发程序设计相比，管程实现很大程度上简化了程序设计。</li>
<li>系统中的各种硬件资源和软件资源，均可用数据结构抽象地描述其资源特性，即用少量信息和对资源所执行的操作来表征该资源，而忽略了它们的内部结构和实现细节。</li>
<li>利用共享数据结构抽象地表示系统中的共享资源，而把对该共享数据结构实施的操作定义为一组过程。</li>
</ul>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://fighterhit.github.io/2018/02/16/Java_Notes/Jvm/性能调优/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Fighter.">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Fighter's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/02/16/Java_Notes/Jvm/性能调优/" itemprop="url">性能调优</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-02-16T23:56:50+08:00">
                2018-02-16
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java笔记/" itemprop="url" rel="index">
                    <span itemprop="name">Java笔记</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="性能调优"><a href="#性能调优" class="headerlink" title="性能调优"></a>性能调优</h2><ul>
<li>知识</li>
<li>工具</li>
<li>数据</li>
<li>经验</li>
</ul>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://fighterhit.github.io/2018/02/13/Java_Notes/Jvm/虚拟机工具/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Fighter.">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Fighter's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/02/13/Java_Notes/Jvm/虚拟机工具/" itemprop="url">虚拟机工具</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-02-13T16:43:40+08:00">
                2018-02-13
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java笔记/" itemprop="url" rel="index">
                    <span itemprop="name">Java笔记</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><ul>
<li>jps</li>
<li>jstat</li>
<li>jinfo</li>
<li>jmap</li>
<li>jhat</li>
<li>jstack</li>
<li>jconsole</li>
</ul>
<h3 id="jps（java-process-status）"><a href="#jps（java-process-status）" class="headerlink" title="jps（java process status）"></a>jps（java process status）</h3><p>本地虚拟机唯一id：lvmid（local virtual machine id）</p>
<blockquote>
<p>jps -l：查看运行的 java 进程的主类全名或 jar 包名称<br>jps -m：查看运行的 java 进程传入主类的参数<br>jps -v：查看运行的 java 进程接收的vm参数</p>
</blockquote>
<h3 id="jstat"><a href="#jstat" class="headerlink" title="jstat"></a>jstat</h3><p>监控——类装载，内存，垃圾收集，JIT 编译的信息</p>
<p><code>jstat -gcutil</code> 查看垃圾收集统计信息</p>
<blockquote>
<p>元空间的本质和永久代类似，都是对 JVM 规范中方法区的实现。不过元空间与永久代之间最大的区别在于：元空间并不在虚拟机中，而是使用本地内存。因此，默认情况下，元空间大小仅受本地内存限制。</p>
</blockquote>
<h3 id="jinfo"><a href="#jinfo" class="headerlink" title="jinfo"></a>jinfo</h3><p>实时查看和调整虚拟机各项参数</p>
<blockquote>
<p>jinfo -flag UseSerialGC 8264<br>jinfo -flag  UseG1GC 8264</p>
</blockquote>
<h3 id="jmap"><a href="#jmap" class="headerlink" title="jmap"></a>jmap</h3><p>JVM Memory Map 命令用于生成 heap dump 文件，如果不使用这个命令，还可以使用 -XX:+HeapDumpOnOutOfMemoryError 参数来让虚拟机出现 OOM 的时候自动生成 dump 文件。 jmap 不仅能生成 dump 文件，还可以查询 finalize 执行队列、Java 堆和永久代的详细信息，如当前使用率、当前使用的是哪种收集器等。</p>
<p><code>jmap -dump:format=b,file=f:\a.bin 13660</code>：生成堆快照文件<br><code>jmap -histo 13660</code>：查看指定 java 进程的类数量和实例数量</p>
<h3 id="jhat（jvm-heap-analysis-tool）"><a href="#jhat（jvm-heap-analysis-tool）" class="headerlink" title="jhat（jvm heap analysis tool）"></a>jhat（jvm heap analysis tool）</h3><p>对上述 jmap 产生的文件进行分析，但太耗 CPU 和内存，因此一般<strong>不常用</strong>。<br><code>jhat f:\a.bin</code><br>Execute Object Query Language (OQL) query<br><code>select s from java.lang.String s where s.value.length &gt; 1000</code></p>
<h3 id="jstack-打印线程堆栈信息"><a href="#jstack-打印线程堆栈信息" class="headerlink" title="jstack 打印线程堆栈信息"></a>jstack 打印线程堆栈信息</h3><p><code>Thread.getAllStackTrace()</code></p>
<h3 id="jconsole-内存监控"><a href="#jconsole-内存监控" class="headerlink" title="jconsole 内存监控"></a>jconsole 内存监控</h3><h4 id="jconsole-内存监控-1"><a href="#jconsole-内存监控-1" class="headerlink" title="jconsole 内存监控"></a>jconsole 内存监控</h4><h4 id="jconsole-线程监控"><a href="#jconsole-线程监控" class="headerlink" title="jconsole 线程监控"></a>jconsole 线程监控</h4><h4 id="jconsole-死锁监控"><a href="#jconsole-死锁监控" class="headerlink" title="jconsole 死锁监控"></a>jconsole 死锁监控</h4><p>点击<code>检测死锁</code>，会看到 等待线程数</p>
<h4 id="VisualVM-与-idea-集成"><a href="#VisualVM-与-idea-集成" class="headerlink" title="VisualVM 与 idea 集成"></a>VisualVM 与 idea 集成</h4><p>安装 <code>VisualVM</code> 插件后，选择本地 jdk 下 bin 目录下的 <code>jvisualvm.exe</code></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://fighterhit.github.io/2018/02/12/Java_Notes/Jvm/垃圾回收与内存分配/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Fighter.">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Fighter's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/02/12/Java_Notes/Jvm/垃圾回收与内存分配/" itemprop="url">垃圾回收与内存分配</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-02-12T11:04:06+08:00">
                2018-02-12
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java笔记/" itemprop="url" rel="index">
                    <span itemprop="name">Java笔记</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="垃圾回收"><a href="#垃圾回收" class="headerlink" title="垃圾回收"></a>垃圾回收</h2><h3 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h3><ul>
<li>如何判定对象为垃圾对象<ul>
<li>引用计数法</li>
<li>可达性分析</li>
</ul>
</li>
<li>如何回收<ul>
<li>回收策略<ul>
<li>标记-清除算法</li>
<li>复制算法</li>
<li>标记-整理算法</li>
<li>分代收集算法</li>
</ul>
</li>
<li>常见垃圾回收器<ul>
<li>Serial</li>
<li>Parnew</li>
<li>Cms</li>
<li>G1</li>
</ul>
</li>
</ul>
</li>
<li>何时回收</li>
</ul>
<h3 id="如何判定对象为垃圾对象"><a href="#如何判定对象为垃圾对象" class="headerlink" title="如何判定对象为垃圾对象"></a>如何判定对象为垃圾对象</h3><h4 id="引用计数法"><a href="#引用计数法" class="headerlink" title="引用计数法"></a>引用计数法</h4><p>在对象中添加一个引用计数器，当有地方引用这个对象的时候，引用计数器的值就+1，当引用失效的时候，计数器的值就+1</p>
<blockquote>
<p>打印 GC 日志 jvm 参数：-verbose:gc -XX:+PrintGCDetails</p>
</blockquote>
<h4 id="可达性分析法"><a href="#可达性分析法" class="headerlink" title="可达性分析法"></a>可达性分析法</h4><ul>
<li>作为 GCRoots 的对象<ul>
<li>虚拟机栈（虚拟机栈中的局部变量表）</li>
<li>方法区类属性所引用的对象</li>
<li>方法区常量所引用的对象</li>
<li>本地方法栈中所引用的对象</li>
</ul>
</li>
</ul>
<h3 id="回收策略"><a href="#回收策略" class="headerlink" title="回收策略"></a>回收策略</h3><h4 id="标记-清除算法"><a href="#标记-清除算法" class="headerlink" title="标记-清除算法"></a>标记-清除算法</h4><ul>
<li>效率问题<ul>
<li>申请内存时找不到可用空间会再触发一次 GC</li>
</ul>
</li>
<li>空间问题<ul>
<li>回收空间不连续</li>
</ul>
</li>
</ul>
<h4 id="复制算法"><a href="#复制算法" class="headerlink" title="复制算法"></a>复制算法</h4><p>解决<code>标记-清除算法</code>的效率问题</p>
<h5 id="回顾内存结构："><a href="#回顾内存结构：" class="headerlink" title="回顾内存结构："></a>回顾内存结构：</h5><ul>
<li>堆<ul>
<li>新生代<ul>
<li>Eden 伊甸园</li>
<li>Survivor 存货区</li>
<li>Tenured Gen</li>
</ul>
</li>
<li>老年代</li>
</ul>
</li>
<li>方法区</li>
<li>栈 本地方法栈 程序计数器</li>
</ul>
<h4 id="标记-整理-清除算法"><a href="#标记-整理-清除算法" class="headerlink" title="标记-整理-清除算法"></a>标记-整理-清除算法</h4><p>主要为了回收老年代</p>
<h4 id="分代收集算法"><a href="#分代收集算法" class="headerlink" title="分代收集算法"></a>分代收集算法</h4><p>针对新生代、老年代分别采用不同的垃圾回收算法，如<br>新生代：复制算法（回收率高）<br>老年代：标记-整理算法（回收率低）</p>
<h3 id="常见垃圾回收器"><a href="#常见垃圾回收器" class="headerlink" title="常见垃圾回收器"></a>常见垃圾回收器</h3><p>不同垃圾回收器适用的场景和区域不同</p>
<h4 id="Serial（串行）收集器"><a href="#Serial（串行）收集器" class="headerlink" title="Serial（串行）收集器"></a>Serial（串行）收集器</h4><ul>
<li>最基本，发展最悠久</li>
<li>单线程垃圾收集器</li>
<li>主要针对新生代内存</li>
<li>桌面应用</li>
</ul>
<h4 id="ParNew-收集器"><a href="#ParNew-收集器" class="headerlink" title="ParNew 收集器"></a>ParNew 收集器</h4><ul>
<li>多线程垃圾收集器</li>
<li>若用 CMS 收集老年代内存，则要用 Serial 或 ParNew 收集新生代内存</li>
</ul>
<h4 id="Parallel-Scavenge-收集器"><a href="#Parallel-Scavenge-收集器" class="headerlink" title="Parallel Scavenge 收集器"></a>Parallel Scavenge 收集器</h4><p>与 ParNew 收集器的异同：</p>
<ul>
<li>复制算法（同，新生代收集器）</li>
<li>多线程收集器（同）</li>
<li><strong>达到可控制的吞吐量</strong>（CPU 运行用户代码的时间与 CPU 消耗总时间的比值，不同），此处 <code>吞吐量 = （执行用户代码时间）/ （执行用户代码时间 + 垃圾回收所占时间）</code></li>
<li>服务端一般更注重吞吐量，而客户端对响应时间要求更高，因此更适合交互较少的服务端</li>
</ul>
<blockquote>
<p>-XX:MaxGCPauseMillis 垃圾收集器停顿时间<br>-XX:GCTimeRatio 吞吐量大小 (0, 100)</p>
</blockquote>
<h4 id="CMS（Concurrent-Mark-Sweep，并发-标记-清除）收集器"><a href="#CMS（Concurrent-Mark-Sweep，并发-标记-清除）收集器" class="headerlink" title="CMS（Concurrent Mark Sweep，并发-标记-清除）收集器"></a>CMS（Concurrent Mark Sweep，并发-标记-清除）收集器</h4><p>用在老年代，减少延迟</p>
<ul>
<li>工作过程<ul>
<li>初始标记</li>
<li>并发标记</li>
<li>重新标记</li>
<li>并发清理</li>
</ul>
</li>
<li>优点<ul>
<li>并发收集</li>
<li>低停顿</li>
</ul>
</li>
<li>缺点<ul>
<li>占用大量 CPU 资源</li>
<li>无法处理浮动垃圾</li>
<li>出现 Concurrent Mode Failure（如在清除时再创建对象但申请内存失败）</li>
<li>空闲碎片（标记-清除算法导致的）</li>
</ul>
</li>
</ul>
<h4 id="G1-收集器"><a href="#G1-收集器" class="headerlink" title="G1 收集器"></a>G1 收集器</h4><h5 id="历史"><a href="#历史" class="headerlink" title="历史"></a>历史</h5><h5 id="优势"><a href="#优势" class="headerlink" title="优势"></a>优势</h5><ul>
<li>并行与并发</li>
<li>分代收集</li>
<li>空间整合</li>
<li>可预测的停顿</li>
</ul>
<h5 id="步骤"><a href="#步骤" class="headerlink" title="步骤"></a>步骤</h5><ul>
<li>初始标记</li>
<li>并发标记</li>
<li>最终标记</li>
<li>筛选回收</li>
</ul>
<blockquote>
<p>RememberSet 表记录对外引用，再加上单独一块区域（region）来进行垃圾回收</p>
</blockquote>
<h5 id="与-CMS-比较"><a href="#与-CMS-比较" class="headerlink" title="与 CMS 比较"></a>与 CMS 比较</h5><h2 id="内存分配"><a href="#内存分配" class="headerlink" title="内存分配"></a>内存分配</h2><ul>
<li>优先分配到 eden</li>
<li>大对象直接分配到老年代</li>
<li>长期存活的对象分配到老年代</li>
<li>空间分配担保</li>
<li>动态对象年龄判断</li>
</ul>
<h3 id="对象优先在-Eden-上分配"><a href="#对象优先在-Eden-上分配" class="headerlink" title="对象优先在 Eden 上分配"></a>对象优先在 Eden 上分配</h3><p><code>java -version</code> 查看 jdk 所处运行环境为 <code>Server VM</code>（多核、内存 &gt; 2G）</p>
<ul>
<li>添加虚拟机参数 <code>-verbose:gc -XX:+PrintGCDetails</code> 可看到默认为 <code>Parallel</code> 垃圾收集器，看到其新生代为 <code>PSYoungGen</code></li>
<li>添加虚拟机参数 <code>-XX:+UseSerialGC</code> 启用 <code>Serial</code> 垃圾收集器，看到其新生代为 <code>def new generation</code></li>
</ul>
<blockquote>
<p><code>GC</code>：主要回收新生代，执行很频繁，耗时短<br><code>Full GC</code>：主要回收老年代，可以人为触发或自动触发，耗时很长</p>
</blockquote>
<img src="/2018/02/12/Java_Notes/Jvm/垃圾回收与内存分配/SurvivorRatio参数含义.png" title="SurvivorRatio参数含义">
<h3 id="大对象直接进入老年代"><a href="#大对象直接进入老年代" class="headerlink" title="大对象直接进入老年代"></a>大对象直接进入老年代</h3><p>大对象一般是大字符串、大数组等，Eden 区域 GC 频率很高，所以每次 GC 移动大对象很耗时，故将其直接分配在老年代，老年代回收频率低</p>
<blockquote>
<p>-XX:PretenureSizeThreshold</p>
</blockquote>
<h3 id="长期存活的对象进入老年代"><a href="#长期存活的对象进入老年代" class="headerlink" title="长期存活的对象进入老年代"></a>长期存活的对象进入老年代</h3><p>一般采用复制算法，当对象被移到 Survivor 区域，对象年龄+1，达到下面设定的阈值后，则将对象移动到老年代</p>
<blockquote>
<p>-XX:MaxTenuringThreshold=15</p>
</blockquote>
<h3 id="空间分配担保"><a href="#空间分配担保" class="headerlink" title="空间分配担保"></a>空间分配担保</h3><p>默认启用了空间分配担保，新生代内存不够向老年代借用内存</p>
<blockquote>
<p>-XX:+HandlePromotionFailure</p>
</blockquote>
<h3 id="逃逸分析与栈上分配"><a href="#逃逸分析与栈上分配" class="headerlink" title="逃逸分析与栈上分配"></a>逃逸分析与栈上分配</h3><p>方法的栈帧随方法结束而出栈被销毁，那么方法体内对象的内存就自然而然被回收，因此若对象没有逃逸，则可随方法结束而被回收。<br>逃逸分析：分析对象作用域。<br>只要对象在方法作用域内就没逃逸。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StackAllocation</span> </span>&#123;</div><div class="line">    <span class="keyword">public</span> StackAllocation obj;</div><div class="line">    <span class="comment">//方法返回 StackAllocation 对象，发生逃逸</span></div><div class="line">    <span class="function"><span class="keyword">public</span> StackAllocation <span class="title">getInstance</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> obj == <span class="keyword">null</span> ? <span class="keyword">new</span> StackAllocation() : obj;</div><div class="line">    &#125;</div><div class="line">    <span class="comment">//为成员属性赋值，发生逃逸</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setObj</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">this</span>.obj = <span class="keyword">new</span> StackAllocation();</div><div class="line">    &#125;</div><div class="line">    <span class="comment">//对象作用域尽在当前方法中有效，没有发生逃逸</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">useStackAllocation</span><span class="params">()</span> </span>&#123;</div><div class="line">        StackAllocation s = <span class="keyword">new</span> StackAllocation();</div><div class="line">    &#125;</div><div class="line">    <span class="comment">// 引用成员变量的值，发生逃逸</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">useStackAllocation2</span><span class="params">()</span> </span>&#123;</div><div class="line">        StackAllocation s = getInstance();</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://fighterhit.github.io/2018/02/11/Java_Notes/Jvm/Java内存区域/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Fighter.">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Fighter's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/02/11/Java_Notes/Jvm/Java内存区域/" itemprop="url">Java内存区域</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-02-11T12:00:27+08:00">
                2018-02-11
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java笔记/" itemprop="url" rel="index">
                    <span itemprop="name">Java笔记</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="Java内存区域"><a href="#Java内存区域" class="headerlink" title="Java内存区域"></a>Java内存区域</h2><h3 id="内存区域简介"><a href="#内存区域简介" class="headerlink" title="内存区域简介"></a>内存区域简介</h3><img src="/2018/02/11/Java_Notes/Jvm/Java内存区域/java内存区域.png" title="java内存区域">
<h3 id="程序计数器"><a href="#程序计数器" class="headerlink" title="程序计数器"></a>程序计数器</h3><ul>
<li>程序计数器是一块较小的内存空间，它可以看作是当前线程所执行的字节码的行号指示器</li>
<li>程序计数器处于线程独占区</li>
<li>如果线程执行的是 Java 方法，这个计数器记录的是正在执行的虚拟机字节码指令的地址。如果正在执行的是 native 方法，这个计数器的值是 undefined</li>
<li>此区域是唯一一个在 Java 虚拟机规范没有设定任何 OutOfMemoryError 情况的区域（因为虚拟机内部处理，开发者不用处理）</li>
</ul>
<h3 id="Java-虚拟机栈"><a href="#Java-虚拟机栈" class="headerlink" title="Java 虚拟机栈"></a>Java 虚拟机栈</h3><ul>
<li>虚拟机栈描述的是 Java 方法执行的动态内存模型</li>
<li><p>栈帧</p>
<ul>
<li><strong>每个方法执行都会创建一个栈帧</strong>，伴随着方法从创建到执行完成。用于存储<strong>局部变量表，操作数栈，动态链接，方法出口</strong>等。</li>
</ul>
</li>
<li><p>局部变量表</p>
<ul>
<li>存放编译期可知的各种基本数据类型，引用类型，returnAddress 等</li>
<li>局部变量表的内存空间在编译期完成分配，当进入一个方法时，这个方法需要在帧分配多少内存是固定的，在方法运行期间是不会改变局部变量表的大小</li>
</ul>
</li>
<li>大小<ul>
<li>StackOverflowError</li>
<li>OutOfMemory</li>
</ul>
</li>
</ul>
<blockquote>
<p>限制堆大小 jvm 参数：-XX:+HeapDumpOnOutOfMemoryError -Xmx5m -Xms5m</p>
</blockquote>
<h3 id="本地方法栈"><a href="#本地方法栈" class="headerlink" title="本地方法栈"></a>本地方法栈</h3><blockquote>
<p>HotSpot 虚拟机的 Java 虚拟机栈和本地方法栈实际上是在一起的，并没有分开</p>
</blockquote>
<ul>
<li>虚拟机栈为虚拟机执行 Java 方法服务</li>
<li>本地方法栈为虚拟机执行 native 方法服务</li>
</ul>
<h3 id="Java-堆"><a href="#Java-堆" class="headerlink" title="Java 堆"></a>Java 堆</h3><ul>
<li>存放对象实例</li>
<li>垃圾收集器管理的主要区域</li>
<li>新生代，老年代，Eden 空间</li>
<li>OutOfMemory 异常</li>
<li>-Xmx -Xms</li>
</ul>
<h3 id="方法区"><a href="#方法区" class="headerlink" title="方法区"></a>方法区</h3><ul>
<li>存储虚拟机加载的类信息，常量，静态变量，即时编译器编译后的代码等数据<ul>
<li>类的版本</li>
<li>字段</li>
<li>方法</li>
<li>接口</li>
</ul>
</li>
<li>方法区和永久代<ul>
<li>两者并不等价，只是为了节省为方法区单独编写内存管理代码</li>
</ul>
</li>
<li>垃圾回收在方法区的行为</li>
<li>异常的定义<ul>
<li>OutOfMemoryError</li>
</ul>
</li>
</ul>
<h3 id="运行时常量池"><a href="#运行时常量池" class="headerlink" title="运行时常量池"></a>运行时常量池</h3><p>属于方法区，</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">fun</span><span class="params">()</span></span>&#123;</div><div class="line">    String s1 = <span class="string">"abc"</span>;<span class="comment">//字节码常量，在常量池创建对象</span></div><div class="line">    String s2 = <span class="string">"abc"</span>;<span class="comment">//在常量池创建对象</span></div><div class="line">    System.out.println(s1 == s2);<span class="comment">//true</span></div><div class="line">    String s3 = <span class="keyword">new</span> String(<span class="string">"abc"</span>);<span class="comment">//在堆创建对象</span></div><div class="line">    System.out.println(s1 == s3);<span class="comment">//false</span></div><div class="line">    System.out.println(s1 == s3.intern());<span class="comment">//true，intern() 方法产生运行时常量，将对象从堆搬到常量池</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="直接内存"><a href="#直接内存" class="headerlink" title="直接内存"></a>直接内存</h3><h3 id="对象的创建"><a href="#对象的创建" class="headerlink" title="对象的创建"></a>对象的创建</h3><img src="/2018/02/11/Java_Notes/Jvm/Java内存区域/创建对象过程.png" title="创建对象过程">
<ul>
<li>给对象分配内存<ul>
<li>指针碰撞</li>
<li>空闲列表</li>
</ul>
</li>
<li>线程安全性问题<ul>
<li>线程同步</li>
<li>本地线程分配缓冲 TLAB</li>
</ul>
</li>
<li>初始化对象</li>
<li>执行构造方法</li>
</ul>
<h3 id="对象的结构"><a href="#对象的结构" class="headerlink" title="对象的结构"></a>对象的结构</h3><ul>
<li>Header<ul>
<li>自身运行时数据（Mark Word，32位 / 64位）<ul>
<li>哈希值</li>
<li>GC 分代年龄</li>
<li>线程持有的锁</li>
<li>偏向线程ID</li>
<li>偏向时间戳</li>
</ul>
</li>
<li>类型指针</li>
</ul>
</li>
<li>InstanceData<ul>
<li>相同宽度类型数据放一起：long/double short/char</li>
</ul>
</li>
<li>Padding<ul>
<li>要求对象起始地址必须是 8 的整数倍</li>
</ul>
</li>
</ul>
<blockquote>
<p>比较著名的垃圾回收算法：</p>
<ol>
<li>标记-清除算法</li>
<li>复制算法</li>
<li>分代收集算法</li>
</ol>
</blockquote>
<h3 id="对象的访问定位"><a href="#对象的访问定位" class="headerlink" title="对象的访问定位"></a>对象的访问定位</h3><ul>
<li>使用句柄</li>
<li>直接指针</li>
</ul>
<blockquote>
<p>保存两个：</p>
<ul>
<li>到对象实例数据的指针</li>
<li>到对象类型数据的指针</li>
</ul>
</blockquote>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://fighterhit.github.io/2018/02/08/algorithm_and_datastructure/Algorithms4/Ch3-Search/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Fighter.">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Fighter's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/02/08/algorithm_and_datastructure/Algorithms4/Ch3-Search/" itemprop="url">（三）查找</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-02-08T21:39:21+08:00">
                2018-02-08
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/算法与数据结构/" itemprop="url" rel="index">
                    <span itemprop="name">算法与数据结构</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <h2 id="符号表"><a href="#符号表" class="headerlink" title="符号表"></a>符号表</h2><p>符号表最主要目的就是将一个<strong>键</strong>和一个<strong>值</strong>联系起来。用例能将一个键值对<strong>插入</strong>符号表并之后能从符号表的所有键值对中按键直接找到对应的值。</p>
<blockquote>
<p>符号表是一种存储键值对的数据结构，支持两种操作：<strong>插入（put）</strong>，即将一组新的键值对存入表中；<strong>查找（get）</strong>，即根据给定的键得到对应的值。</p>
</blockquote>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/2018/02/08/algorithm_and_datastructure/Algorithms4/Ch3-Search/#more" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://fighterhit.github.io/2018/02/08/Java_Notes/Java/JDK8-新特性/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Fighter.">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Fighter's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/02/08/Java_Notes/Java/JDK8-新特性/" itemprop="url">JDK8-新特性</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-02-08T18:42:14+08:00">
                2018-02-08
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java笔记/" itemprop="url" rel="index">
                    <span itemprop="name">Java笔记</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="JDK8的新特性"><a href="#JDK8的新特性" class="headerlink" title="JDK8的新特性"></a>JDK8的新特性</h2><p>Java 8 使用两个概念扩展了接口的含义：<strong>默认方法和静态方法</strong>。<br>默认方法使得开发者可以在不破坏二进制兼容性的前提下，往现存接口中添加新的方法，即不强制那些实现了该接口的类也同时实现这个新加的方法。</p>
<h3 id="接口的默认方法和静态方法"><a href="#接口的默认方法和静态方法" class="headerlink" title="接口的默认方法和静态方法"></a>接口的默认方法和静态方法</h3><h3 id="Lambda-表达式和函数式编程"><a href="#Lambda-表达式和函数式编程" class="headerlink" title="Lambda 表达式和函数式编程"></a>Lambda 表达式和函数式编程</h3><p>Lambda 表达式可以来代替匿名内部类</p>
<h3 id="Date-API"><a href="#Date-API" class="headerlink" title="Date API"></a>Date API</h3><h3 id="重复注解"><a href="#重复注解" class="headerlink" title="重复注解"></a>重复注解</h3><h3 id="更好的类型推断"><a href="#更好的类型推断" class="headerlink" title="更好的类型推断"></a>更好的类型推断</h3><p>Java 8 编译器在类型判断方面有很大的提升，在很多场景下编译器可以推导出某个参数的数据类型，从而使得代码更为简洁。</p>
<h3 id="Nashorn-JavaScript引擎"><a href="#Nashorn-JavaScript引擎" class="headerlink" title="Nashorn JavaScript引擎"></a>Nashorn JavaScript引擎</h3><p>使用 Metaspace（JEP 122）代替持久代（PermGen space）。在 JVM 参数方面，使用 -XX:MetaSpaceSize 和 -XX:MaxMetaspaceSize 代替原来的 -XX:PermSize 和 -XX:MaxPermSize。</p>
<h2 id="Java-虚拟机发展"><a href="#Java-虚拟机发展" class="headerlink" title="Java 虚拟机发展"></a>Java 虚拟机发展</h2><ul>
<li>Sun Classic VM<ul>
<li>世界上第一款商用的 Java 虚拟机</li>
<li>只能用纯解释器的方式来执行 Java 代码</li>
</ul>
</li>
<li>Exact VM<ul>
<li>Exact Memory Management 准确试内存管理（知道虚拟机某个位置数据的类型，如整数是数值还是地址，在垃圾回收时可判断堆上内存是否还被使用）</li>
<li>编译器和解释器混合工作以及两级即时编译器</li>
<li>只在 Solaris 平台发布</li>
</ul>
</li>
<li>HotSpot VM<ul>
<li>HotSpot VM 的热点代码探测能力可通过执行计数器找出最具有编译价值的代码，然后通知 JIT 编译器以方法为单位进行编译。若一个方法被频繁调用，或方法中有效循环次数很多，将会分别触发标准编译和 OSR （栈上替换） 编译动作。通过编译器和解释器恰当地协同工作，可在最优化的程序响应时间与最佳执行性能中取得平衡，而且无须等待本地代码输出才能执行程序，即时编译压力也相对减小，这样有助于引入更多代码优化技术，输出质量更高的本地代码。</li>
</ul>
</li>
<li>KVM（Kilobyte）<ul>
<li>Kilobyte 简单，轻量，高度可移植</li>
<li>在手机平台运行</li>
</ul>
</li>
<li>JRockit<ul>
<li>BEA</li>
<li>世界上最快的 Java 虚拟机</li>
<li>专注服务器端应用</li>
<li>优势<ul>
<li>垃圾收集器</li>
<li>MissionControl 服务套件</li>
</ul>
</li>
</ul>
</li>
<li>J9</li>
<li>Azul VM</li>
<li>Liquid VM</li>
<li>Dalvik VM</li>
<li>Microsoft VM</li>
</ul>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://fighterhit.github.io/2018/01/29/algorithm_and_datastructure/Algorithms4/Ch2-Sort/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Fighter.">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Fighter's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/01/29/algorithm_and_datastructure/Algorithms4/Ch2-Sort/" itemprop="url">（三）排序</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-01-29T20:08:19+08:00">
                2018-01-29
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/算法与数据结构/" itemprop="url" rel="index">
                    <span itemprop="name">算法与数据结构</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <h2 id="1-初级排序算法"><a href="#1-初级排序算法" class="headerlink" title="1 初级排序算法"></a>1 初级排序算法</h2><p>排序算法关注的主要是重新排列数组元素，其中每个元素都有一个主键。排序算法是将所有元素主键按某种方式排列（通常是按照大小或是字母顺序）。排序后索引较大的主键大于等于索引较小的主键。</p>
<h3 id="排序算法类的模板"><a href="#排序算法类的模板" class="headerlink" title="排序算法类的模板"></a><span id="example"></span>排序算法类的模板</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Example</span></span>&#123;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">sort</span><span class="params">(Comparable a[])</span></span>&#123;&#125;</div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">less</span><span class="params">(Comparable a, Comparable b)</span></span>&#123;</div><div class="line">        <span class="keyword">return</span> a.compareTo(b) &lt; <span class="number">0</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">exch</span><span class="params">(Comparable[] a, <span class="keyword">int</span> i, <span class="keyword">int</span> j)</span></span>&#123;</div><div class="line">        Comparable t = a[i];</div><div class="line">        a[i] = a[j];</div><div class="line">        a[j] = t;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">show</span><span class="params">(Comparable[] a)</span></span>&#123;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>;  i &lt; a.length; i++)&#123;</div><div class="line">            System.out.print(a[i] + <span class="string">" "</span>);</div><div class="line">        &#125;</div><div class="line">        System.out.println();</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">isSort</span><span class="params">(Comparable[] a)</span></span>&#123;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; a.length; i++)&#123;</div><div class="line">            <span class="keyword">if</span>(less(a[i], a[i - <span class="number">1</span>]))</div><div class="line">                <span class="keyword">return</span> <span class="keyword">false</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</div><div class="line">        <span class="comment">//从标准输入读入字符串，排序后输出</span></div><div class="line">        Integer[] a = <span class="keyword">new</span> Integer[]&#123;<span class="number">1</span>,<span class="number">34</span>,<span class="number">55</span>,<span class="number">66</span>,<span class="number">7</span>&#125;;</div><div class="line">        sort(a);</div><div class="line">        <span class="function"><span class="keyword">assert</span> <span class="title">isSort</span><span class="params">(a)</span></span>;</div><div class="line">        show(a);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>排序成本模型：研究排序算法时，需要计算<strong>比较和交换</strong>的次数。对于不交换元素的算法，计算<strong>访问数组的次数</strong>。</li>
<li>额外内存使用：排序算法的额外内存开销和运行时间同等重要。排序算法可分两类：除了函数调用所需的栈和固定数目的实例变量之外无需额外内存的<strong>原地排序算法</strong>，以及需要<strong>额外内存空间</strong>来存储另一份数组副本的其它排序算法。</li>
<li>数据类型：上述排序算法模板适用于任何实现了Comparable接口的数据类型。例如，Java中封装的Integer和Double，以及String和其他许多高级数据类型（如File和URL）都实现了Comparable接口，因此可以直接调用这些类型的数组作为参数调用我们自己实现的排序方法。</li>
</ul>
</blockquote>
<p>例如——用快排对N个随机的Double数据进行排序：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">Doulbe[] a = <span class="keyword">new</span> Double[N];</div><div class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; N; i++)&#123;</div><div class="line">    a[i] = StdRandom.uniform();</div><div class="line">    Quick.sort(a);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/2018/01/29/algorithm_and_datastructure/Algorithms4/Ch2-Sort/#more" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://fighterhit.github.io/2017/12/30/algorithm_and_datastructure/Algorithms4/Ch1-Fundamentals-Time-Complexity/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Fighter.">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Fighter's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/12/30/algorithm_and_datastructure/Algorithms4/Ch1-Fundamentals-Time-Complexity/" itemprop="url">（二）基础（Fundamentals）：算法分析</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-12-30T23:59:11+08:00">
                2017-12-30
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/算法与数据结构/" itemprop="url" rel="index">
                    <span itemprop="name">算法与数据结构</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <h2 id="观察"><a href="#观察" class="headerlink" title="观察"></a>观察</h2><p>运行时间和输入本身相对无关，主要取决于问题规模<br>例：统计文件中三个数和为0的数量</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ThreeSum</span></span>&#123;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">count</span><span class="params">(<span class="keyword">int</span> a[])</span></span>&#123;</div><div class="line">        <span class="keyword">int</span> N = a.length, cnt = <span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; N; i++)&#123;</div><div class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j = i + <span class="number">1</span>; j &lt; N; j++)&#123;</div><div class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> k = j + <span class="number">1</span>; k  &lt; N; k++)&#123;</div><div class="line">                    <span class="keyword">if</span>(a[i] + a[j] + a[k] == <span class="number">0</span>)</div><div class="line">                        cnt += <span class="number">1</span>;</div><div class="line">                &#125;</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>一种表示计时器的抽象数据类型：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StopWatch</span></span>&#123;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">long</span> start;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="title">StopWatch</span><span class="params">()</span></span>&#123;</div><div class="line">        start = System.currentTimeMillis();</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">double</span> <span class="title">elapsedTime</span><span class="params">()</span></span>&#123;</div><div class="line">        <span class="keyword">long</span> now = System.currentTimeMillis();</div><div class="line">        <span class="keyword">return</span> (now - start) / <span class="number">1000.0</span>;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/2017/12/30/algorithm_and_datastructure/Algorithms4/Ch1-Fundamentals-Time-Complexity/#more" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://fighterhit.github.io/2017/12/27/algorithm_and_datastructure/Algorithms4/Ch1-Fundamentals/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Fighter.">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Fighter's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/12/27/algorithm_and_datastructure/Algorithms4/Ch1-Fundamentals/" itemprop="url">（一）基础（Fundamentals）：数据结构</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-12-27T14:21:56+08:00">
                2017-12-27
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/算法与数据结构/" itemprop="url" rel="index">
                    <span itemprop="name">算法与数据结构</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <h2 id="1-基础编程模型"><a href="#1-基础编程模型" class="headerlink" title="1 基础编程模型"></a>1 基础编程模型</h2><h3 id="Java-四类八种类型"><a href="#Java-四类八种类型" class="headerlink" title="Java 四类八种类型"></a>Java 四类八种类型</h3><ul>
<li>整型：byte, short, int, long</li>
<li>浮点型：float, double</li>
<li>布尔型：boolean</li>
<li>字符型：char</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">binarySearch</span><span class="params">(<span class="keyword">int</span>[] a, <span class="keyword">int</span> key)</span></span>&#123;</div><div class="line">    <span class="keyword">int</span> left = <span class="number">0</span>;</div><div class="line">    <span class="keyword">int</span> right = a.length - <span class="number">1</span>;</div><div class="line">    <span class="keyword">while</span>(left &lt;= right)&#123;</div><div class="line">        <span class="keyword">int</span> mid = left + ((right - left) &gt;&gt;<span class="number">1</span>);</div><div class="line">        <span class="keyword">if</span>(a[mid] &gt; key)&#123;</div><div class="line">            right = mid -<span class="number">1</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">else</span> <span class="keyword">if</span>(a[mid] &lt; key)&#123;</div><div class="line">            left = mid + <span class="number">1</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">else</span>&#123;</div><div class="line">            <span class="keyword">return</span> mid;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> -<span class="number">1</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h3><ol>
<li><p>颠倒数组中元素顺序</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//注意 n 值、for 循环边界、以及循环体内 n - 1 - i</span></div><div class="line"><span class="keyword">int</span> n = a.length;</div><div class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n/<span class="number">2</span>; i++)&#123;</div><div class="line">    <span class="keyword">int</span> temp = a[i];</div><div class="line">    a[i] = a[n - <span class="number">1</span> - i];</div><div class="line">    a[i - <span class="number">1</span> - i] = temp;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
</li>
<li><p>矩阵相乘</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//假设a[n][n], b[n][n], c[n][n]</span></div><div class="line"><span class="keyword">int</span> n = a.length;</div><div class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++)&#123;</div><div class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; n; j++)&#123;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> k = <span class="number">0</span>; k &lt; n; k++)&#123;</div><div class="line">            c[i][j] += a[i][k] * b[k][j];</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
</li>
</ol>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/2017/12/27/algorithm_and_datastructure/Algorithms4/Ch1-Fundamentals/#more" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  
  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/"><i class="fa fa-angle-left"></i></a><a class="page-number" href="/">1</a><span class="page-number current">2</span><a class="page-number" href="/page/3/">3</a><a class="extend next" rel="next" href="/page/3/"><i class="fa fa-angle-right"></i></a>
  </nav>



          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.jpg"
                alt="Fighter." />
            
              <p class="site-author-name" itemprop="name">Fighter.</p>
              <p class="site-description motion-element" itemprop="description">学习 | 分享 | 交流 | 进步</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">26</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">28</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          <div class="links-of-author motion-element">
            
              
                <span class="links-of-author-item">
                  <a href="https://github.com/fighterhit" target="_blank" title="GitHub">
                    
                      <i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://weibo.com/fighterhit" target="_blank" title="Weibo">
                    
                      <i class="fa fa-fw fa-weibo"></i>Weibo</a>
                </span>
              
            
          </div>

          
          

          
          

          

        </div>
      </section>

      

      
      
    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2016 - <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-home"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Fighter.</span>

  
</div>









        
<div class="busuanzi-count">
  <!--<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>-->
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      
    </span>
  

    <span class="site-pv">&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;
    Hosted by <a target="_blank" href="https://github.com/">GitHub Pages</a>
    </span>

</div>







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.3"></script>



  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  




	





  





  








  





  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end -->
  


  

  

</body>
</html>
